package com.stylefeng.guns.modular.biz.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Test;
import com.stylefeng.guns.modular.system.warpper.DictWarpper;
import com.stylefeng.guns.modular.biz.service.ITestService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.plugins.Page;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.stylefeng.guns.modular.system.jqgrid.ExcelUtil;
import com.stylefeng.guns.modular.system.jqgrid.ExportPage;
import com.stylefeng.guns.modular.system.jqgrid.FrontPage;
import com.stylefeng.guns.modular.system.jqgrid.CustomPage;

/**
 * 功能测试控制器
 *
 * @author Greedwind
 * @Date 2018-10-26 09:59:59
 */
@Controller
@RequestMapping("/test")
public class TestController extends BaseController {

    private String PREFIX = "/biz/test/";

    @Autowired
    private ITestService testService;

    /**
     * 跳转到功能测试首页 
     */
    @RequestMapping("")
    public String index(Model model) {
		//controller输出select列表格式: 值1:描述1;值2:描述2
		//注意最前面有个“:;”; 表示可以选择空
		//	如：:;0:否;1:是 
    	model.addAttribute("select_var_fdSelect",  DictWarpper.getDictSingleSelectItemsByCode("country"));	//请传递此值到页面
    	model.addAttribute("select_var_fdSelect2", DictWarpper.getDictMultiSelectItemsByCode("country"));	//请传递此值到页面
        return PREFIX + "test.html";
    }

    /**
     * 跳转到添加功能测试
     */
    @RequestMapping("/test_add")
    public String testAdd() {
        return PREFIX + "test_add.html";
    }

    /**
     * 跳转到修改功能测试
     */
    @RequestMapping("/test_update/{testId}")
    public String testUpdate(@PathVariable Integer testId, Model model) {
        Test test = testService.selectById(testId);
        model.addAttribute("item",test);
        LogObjectHolder.me().set(test);
        return PREFIX + "test_edit.html";
    }


    /**
     * 获取功能测试列表(jqgrid)
     */
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list2(FrontPage<Test> page, HttpServletRequest request) {
		Page<Test> pageList = testService.selectPage(page.getPagePlus(),page.getWrapper());
		CustomPage<Test> customPage = new CustomPage<Test>(pageList);
		return customPage;
	}

    /**
   	 *导出Excel(jqgrid)
     */
	@ResponseBody
	@RequestMapping(value="/Export2Excel")
	public void Export2Excel(HttpServletRequest req,  HttpServletResponse res) {
		 String params = req.getParameter("export2excel_params");
		//json字串转POJO
		ExportPage<Test> page = JSON.parseObject(params, new TypeReference<ExportPage<Test>>(){});
		List<Map<String, Object>> list = testService.selectMaps(page.getWrapper());
        Map<String,String> map = new HashMap<String, String>();
		try {
			ExcelUtil<Test> export = new ExcelUtil<Test>();
			export.exportToExcel(res, list, page);
		} catch (Exception e) {
            e.printStackTrace();
		}
	}   	
	
    /**
     * 新增功能测试
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Test test) {
        testService.insert(test);
        return SUCCESS_TIP;
    }

    /**
     * 删除功能测试
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer testId) {
        testService.deleteById(testId);
        return SUCCESS_TIP;
    }

    /**
     * 修改功能测试
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Test test) {
        testService.updateById(test);
        return SUCCESS_TIP;
    }

    /**
     * 功能测试详情
     */
    @RequestMapping(value = "/detail/{testId}")
    @ResponseBody
    public Object detail(@PathVariable("testId") Integer testId) {
        return testService.selectById(testId);
    }
}
